import { EntityState, EntityStore, PaginatorPlugin, QueryEntity, StoreConfig } from '@datorama/akita';
import { produce } from 'immer';
import { AudienceItem } from '@/models/response/roomRsp';
import { AudienceSearchType } from '@/models/LiveRoom';

export interface AudienceEntity extends EntityState<AudienceItem, string> {
  ui: AudienceSearchType;
}

const initialState = {
  ui: {searchKey: "" }
};

@StoreConfig({ name: "audiences", producerFn: produce, idKey: 'userId' })
export class AudiencesStore extends EntityStore<AudienceEntity> {
  constructor() {
    super(initialState);
  }

  //UI属性传入
  updateUI(searchKey?: string) {
    this.update({ ui: { searchKey } })
  }
}

export class AudiencesQuery extends QueryEntity<AudienceEntity> {
  constructor(protected store: AudiencesStore) {
    super(store);
  }

  get UI(): AudienceSearchType {
    return this.getValue().ui;
  }
}

export const makeStore = (): [AudiencesStore, AudiencesQuery, PaginatorPlugin<AudienceEntity>] => {
  const store = new AudiencesStore();
  const query = new AudiencesQuery(store);

  const rooms_paginator = new PaginatorPlugin(query)
    .withControls()
    .withRange();

  return [store, query, rooms_paginator];
};
